
Download Project (Full Project)
Step-1
In this tutorial I am going to show you “Search functionality” how to implement search functionality in ASP.NET MVC. First open visual studio then create a ASP.NET web application. Then Select MVC and click ok. Now create database in SQL server management studio. Now connect ADO.NET Entity framework and connect database. Now build the project. Under the table have some property. First create database object. Then the Home controller under index method find the search value and pass the view. Given bellow the controller code:
using Repository_Pattern.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Repository_Pattern.Controllers
{
public class SearchFunctionalityController : Controller
{
OrclDbContext db = new OrclDbContext();
// GET: SearchFunctionality
public ActionResult Index(string search)
{
return View(db.UserInfo.Where(x=>x.Name.Contains(search)).ToList());
}
}
}
Step-2
Now “BegunForm” method under view posts the input search value by the rezor syntax. Also find value show the view under table tag. When data is not find then the red message show on the view otherwise find the value and show the view by using foreach loop under table tag. Given bellow the view code:
@model IEnumerable<Repository_Pattern.Data.UserInfo>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@using (@Html.BeginForm())
{
@Html.TextBox("search") <input type="submit" value="search" />
}
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Hour)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Hour)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
</table>
Step-2
Now build and run the project.